﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data.Objects.SqlClient;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Common.Enums.Data;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.BaseQueryModel;
using ZBJF.Warranty.Model.ViewModel.InsuranceProducts;
using ZBJF.Warranty.Model.ViewModel.Product;

namespace ZBJF.Warranty.BLL
{
    public partial class InsuranceProductsService
    {
        public InsuranceProducts LoadByID(long id)
        {
            return LoadEntities(o => o.ID == id).FirstOrDefault();
        }

        public bool IsExist(string productName, long id = 0)
        {
            var expressionWhere = PredicateExtensions.True<InsuranceProducts>();
            expressionWhere = expressionWhere.And(o => o.ProductName.Equals(productName));
            if (id != 0)
                expressionWhere = expressionWhere.And(o => o.ID != id);
            return LoadEntities(expressionWhere).Any();
        }

        #region 分页获取保险产品列表 + IQueryable<InsuranceProducts> GetPagingList(InsuranceProductsModel model, out int total)
        public IQueryable<InsuranceProducts> GetPagingList(InsuranceProductsModel model, out int total)
        {
            var expressionWhere = PredicateExtensions.True<InsuranceProducts>();
            if (model.IsAvailable.HasValue)
            {
                expressionWhere = expressionWhere.And(c => c.IsAvailable == model.IsAvailable);
            }
            if (model.CoverageStart.HasValue)
            {
                expressionWhere = expressionWhere.And(c => c.Coverage >= model.CoverageStart);
            }
            if (model.CoverageEnd.HasValue)
            {
                expressionWhere = expressionWhere.And(c => c.Coverage <= model.CoverageEnd);
            }
            if (!string.IsNullOrWhiteSpace(model.ProductName))
            {
                expressionWhere = expressionWhere.And(o => o.ProductName.Contains(model.ProductName.Trim()));
            }
            if (string.IsNullOrEmpty(model.iSorts))
            {
                model.iSorts = "ID DESC";
            }

            return LoadPagerEntities<InsuranceProducts>(model.PageIndex, model.iDisplayLength, out total, expressionWhere, model.iSorts);
        }
        #endregion


        /// <summary>
        /// 获取所有产品
        /// </summary>
        /// <returns></returns>
        public IQueryable<InsuranceProducts> GetInsuranceProductsAll()
        {
            return LoadEntities(o => true);
        }
        public IQueryable<InsuranceProducts> LoadAllInsuranceProductsBy(long sellerInsuranceProductId)
        {
            var expressionWhere = PredicateExtensions.True<InsuranceProducts>();

            var insuranceProductsIds =
                ServiceFactory.SellerInsuranceProductMappingsServiceInstance.GetAllInsuranceProductIDs(
                    sellerInsuranceProductId);

            expressionWhere = expressionWhere.And(o => insuranceProductsIds.Contains(o.ID) && o.IsAvailable);

            return LoadEntities(expressionWhere);
        }

        /// <summary>
        /// 根据商家产品ID获取其中是打包产品的基础产品
        /// </summary>
        /// <param name="sellerInsuranceProductId"></param>
        /// <returns></returns>
        public IQueryable<InsuranceProducts> LoadPackageInsuranceProductsBy(long sellerInsuranceProductId)
        {
            var expressionWhere = PredicateExtensions.True<InsuranceProducts>();
            var insuranceProductsIds =
                ServiceFactory.SellerInsuranceProductMappingsServiceInstance.GetPackageInsuranceProductIDs(sellerInsuranceProductId);

            expressionWhere = expressionWhere.And(o => insuranceProductsIds.Contains(o.ID) && o.IsAvailable);

            return LoadEntities(expressionWhere);
        }

        /// <summary>
        /// 根据商家产品ID获取其中不是是打包产品的基础产品
        /// </summary>
        /// <param name="sellerInsuranceProductId"></param>
        /// <returns></returns>
        public IQueryable<InsuranceProducts> LoadNotPackageInsuranceProductsBy(long sellerInsuranceProductId)
        {
            var expressionWhere = PredicateExtensions.True<InsuranceProducts>();
            var insuranceProductsIds = ServiceFactory.SellerInsuranceProductMappingsServiceInstance.GetNotPackageInsuranceProductIDs(sellerInsuranceProductId);
            expressionWhere = expressionWhere.And(o => insuranceProductsIds.Contains(o.ID) && o.IsAvailable);
            return LoadEntities(expressionWhere);
        }

        #region 获取指定价格在价格范围内的基础产品ID集合 + IQueryable<InsuranceProducts> GetInsuranceProductsInPointAmount(decimal productAmount)
        /// <summary>
        /// 获取指定价格在价格范围内的基础产品ID集合
        /// </summary>
        /// <param name="productAmount"></param>
        /// <returns></returns>
        public List<int> GetInsuranceProductsInPointAmount(decimal productAmount)
        {
            var expressionWhere = PredicateExtensions.True<CellPhoneServiceProject>();
            expressionWhere = expressionWhere.And(o => productAmount >= o.ProductAmountFrom && productAmount <= o.ProductAmountTo);
            return (from a in ServiceFactory.CellPhoneServiceProjectServiceInstance.LoadEntities(expressionWhere)
                    join b in LoadEntities(t => true) on a.InsuranceProductsID equals b.ID
                    select b.ID).ToList();
        }
        #endregion

        #region 获取保险产品信息 + InsuranceProducts GetInsuranceProductsInfo(long sellerInsuranceProductsId)
        /// <summary>
        /// 获取保险产品信息(生效等待期/保险期限/保险期限单位)
        /// </summary>
        /// <param name="sellerInsuranceProductsId">销售商商品ID</param>
        /// <returns></returns>
        public CellPhoneServiceProductInfo GetInsuranceProductsInfo(long sellerInsuranceProductsId)
        {
            return (from a in ServiceFactory.SellerInsuranceProductMappingsServiceInstance.LoadEntities(t => t.SellerInsuranceProductId == sellerInsuranceProductsId)
                    join b in LoadEntities(t => true) on a.InsuranceProductId equals b.ID
                    join c in ServiceFactory.CellPhoneServiceProjectServiceInstance.LoadEntities(t => true) on b.ID equals c.InsuranceProductsID
                    select new
                    {
                        b.WaitDays,
                        b.ID,
                        b.PolicyPeriodYear,
                        b.PolicyPeriodMonth,
                        b.PolicyPeriodDay,
                        c.ServiceProjectId,
                        b.ProductName,
                        c.InsuranceDeviceType,
                        b.Coverage
                    }).ToList().Select(t => new CellPhoneServiceProductInfo()
                    {
                        WaitDays = t.WaitDays,
                        ID = t.ID,
                        PolicyPeriodYear = t.PolicyPeriodYear,
                        PolicyPeriodMonth = t.PolicyPeriodMonth,
                        PolicyPeriodDay = t.PolicyPeriodDay,
                        ProductName = t.ProductName,
                        InsuranceDeviceType = t.InsuranceDeviceType,
                        InsuredAmount = t.Coverage
                    }).FirstOrDefault();
        }
        #endregion

        #region 获取保险产品的可维修配件等级列表 + List<RepairAccessoriesLevelInfo> GetRepairAccessoriesLevelInfo(long insuranceProductID)
        /// <summary>
        /// 获取保险产品的可维修配件等级列表
        /// </summary>
        /// <param name="insuranceProductID">保险产品服务项目ID</param>
        /// <returns></returns>
        public List<RepairAccessoriesLevelInfo> GetRepairAccessoriesLevelInfo(int cellPhoneServiceProjectID)
        {
            return (from a in ServiceFactory.InsuranceProductScreenSPsServiceInstance.LoadEntities(t => t.CellPhoneServiceProjectID == cellPhoneServiceProjectID)
                    join b in ServiceFactory.AccessoriesLevelServiceInstance.LoadEntities(t => true) on a.AccessoriesLevelID equals b.ID into temp1
                    from c in temp1.DefaultIfEmpty()
                    join d in ServiceFactory.AccessoriesTypeServiceInstance.LoadEntities(t => true) on c.AccessoriesTypeID equals d.ID into temp2
                    from e in temp2.DefaultIfEmpty()
                    join f in ServiceFactory.ProductTypeServiceInstance.LoadEntities(t => true) on e.ProductTypeID equals f.ID into temp3
                    from g in temp3.DefaultIfEmpty()
                    select new RepairAccessoriesLevelInfo
                    {
                        AccessoriesLevelName = c.AccessoriesLevelName,
                        AccessoriesTypeName = e.AccessoriesName,
                        ProductTypeName = g.ProductName
                    }).ToList();
        }
        #endregion


        #region 新增基础产品_新 + bool AddInsuranceProductsBase(InsuranceProductsBase param)
        /// <summary>
        /// 新增基础产品_新
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public bool AddInsuranceProductsBase(InsuranceProductsBase param)
        {
            InsuranceProducts insuranceProduct = new InsuranceProducts()
            {
                InsuranceProductNo = "SP" + CommonMethod.GetTimeStamp(),
                Coverage = param.Coverage,
                ProductName = param.ProductName,
                PolicyPeriodYear = param.PolicyPeriodYear,
                PolicyPeriodMonth = param.PolicyPeriodMonth,
                PolicyPeriodDay = param.PolicyPeriodDay,
                CreateTime = param.CreateTime,
                CreateBy = param.CreateBy,
                WaitDays=param.WaitDays,
                LastModifyBy = param.LastModifyBy,
                LastModifyTime = param.LastModifyTime,
                IsAvailable = param.IsAvailable,
            };
            var cellPhoneService = ServiceFactory.CellPhoneServiceProjectServiceInstance; //手机维修服务
            var screenSPs = ServiceFactory.InsuranceProductScreenSPsServiceInstance;   //2.1 配件等级
            var warrantyMaintenanceTypes = ServiceFactory.InsuranceProductWarrantyMaintenanceTypesServiceInstance; //2.2 添加可维修方式
            var modelType = ServiceFactory.InsuranceProductsAvailableModelTypeServiceInstance;   //2.3 添加机型承保条件
            var screenType = ServiceFactory.InsuranceProductsAvailableScreenTypeServiceInstance;  //2.4 添加屏幕类型承保条件
            var collectItemMappings = ServiceFactory.InsuranceProductsInfoCollectItemMappingsServiceInstance;  //2.5 添加信息收集配置
            var auditProjectConfigurationMappings = ServiceFactory.InsuranceProductsAuditProjectConfigurationMappingsServiceInstance;  //2.6 添加审核项目配置

            using (TransactionScope ts = new TransactionScope())
            {
                try
                {
                    //1、添加保险基础产品信息
                    insuranceProduct = AddEntities(insuranceProduct);
                    if (insuranceProduct.ID < 1)
                    {
                        ts.Dispose();
                        return false;
                    }

                    //2、添加手机维修服务信息
                    #region 添加手机维修服务信息
                    foreach (CellPhoneServiceProjectParam cellPhoneItem in param.CellPhoneServiceProject)
                    {
                        CellPhoneServiceProject cellModel = new CellPhoneServiceProject()
                        {
                            InsuranceProductsID = insuranceProduct.ID,
                            InsuranceDevice = cellPhoneItem.InsuranceDevice,
                            InsuranceDeviceType = cellPhoneItem.InsuranceDeviceType,
                            ModelTypeJudgeType = cellPhoneItem.ModelTypeJudgeType,
                            NewMachineInsuranceTimeLimit = cellPhoneItem.NewMachineInsuranceTimeLimit,
                            ProductAmountFrom = cellPhoneItem.ProductAmountFrom,
                            ProductAmountTo = cellPhoneItem.ProductAmountTo,
                            ScreenTypeJudgeType = cellPhoneItem.ScreenTypeJudgeType,
                            ServiceProjectId = cellPhoneItem.ServiceProjectId,
                            ServiceTimes = cellPhoneItem.ServiceTimes,
                            UseModelTypeOption = cellPhoneItem.UseModelTypeOption,
                            UsePriceOption = cellPhoneItem.UsePriceOption,
                            UseScreenTypeOption = cellPhoneItem.UseScreenTypeOption,
                            InsuredAmount = cellPhoneItem.InsuredAmount
                        };
                        cellModel = cellPhoneService.AddEntities(cellModel);
                        if (cellModel.ID < 1)
                        {
                            ts.Dispose();
                            LogHelper.WriteLog("新增手机维修服务信息失败：" + JsonConvert.SerializeObject(cellModel), LogType.remark);
                            return false;
                        }
                        //2.1 添加配件等级
                        if (cellPhoneItem.AccessoriesLevelList != null)
                        {
                            foreach (InsuranceProductScreenSPs screenSPsItem in cellPhoneItem.AccessoriesLevelList)
                            {
                                screenSPsItem.CellPhoneServiceProjectID = cellModel.ID;
                                var screenSPsRe = screenSPs.AddEntities(screenSPsItem);
                                if (screenSPsRe.ID < 1)
                                {
                                    ts.Dispose();
                                    LogHelper.WriteLog("新增配件等级失败：" + JsonConvert.SerializeObject(screenSPsItem), LogType.remark);
                                    return false;
                                }

                            }
                        }
                        //2.2 添加可维修方式
                        if (cellPhoneItem.InsuranceWarrantyMaintenanceTypeList != null)
                        {
                            foreach (int warrantyMaintenanceTypeItem in cellPhoneItem.InsuranceWarrantyMaintenanceTypeList)
                            {
                                InsuranceProductWarrantyMaintenanceTypes warrantyMaintenanceTypeModel = new InsuranceProductWarrantyMaintenanceTypes
                                {
                                    CellPhoneServiceProjectID = cellModel.ID,
                                    InsuranceWarrantyMaintenanceTypeId = warrantyMaintenanceTypeItem
                                };
                                warrantyMaintenanceTypeModel = warrantyMaintenanceTypes.AddEntities(warrantyMaintenanceTypeModel);
                                if (warrantyMaintenanceTypeModel.ID < 1)
                                {
                                    ts.Dispose();
                                    LogHelper.WriteLog("新增可维修方式失败：" + JsonConvert.SerializeObject(warrantyMaintenanceTypeModel), LogType.remark);
                                    return false;
                                }
                            }
                        }
                        //2.3 添加机型承保条件
                        if (cellPhoneItem.InsuranceProductsAvailableModelType != null)
                        {
                            foreach (int warrantyMaintenanceTypeItem in cellPhoneItem.InsuranceProductsAvailableModelType)
                            {
                                InsuranceProductsAvailableModelType insuranceProductsAvailableModelType = new InsuranceProductsAvailableModelType
                                {
                                    CellPhoneServiceProjectID = cellModel.ID,
                                    ModelID = warrantyMaintenanceTypeItem
                                };
                                insuranceProductsAvailableModelType = modelType.AddEntities(insuranceProductsAvailableModelType);
                                if (insuranceProductsAvailableModelType.ID < 1)
                                {
                                    ts.Dispose();
                                    LogHelper.WriteLog("新增机型承保条件失败：" + JsonConvert.SerializeObject(insuranceProductsAvailableModelType), LogType.remark);
                                    return false;
                                }
                            }
                        }
                        //2.4 添加屏幕类型承保条件
                        if (cellPhoneItem.InsuranceProductsAvailableScreenType != null)
                        {
                            foreach (byte screenTypeItem in cellPhoneItem.InsuranceProductsAvailableScreenType)
                            {
                                InsuranceProductsAvailableScreenType insuranceProductsAvailableScreenType = new InsuranceProductsAvailableScreenType
                                {
                                    CellPhoneServiceProjectID = cellModel.ID,
                                    ScreenTypeCode = screenTypeItem
                                };
                                insuranceProductsAvailableScreenType = screenType.AddEntities(insuranceProductsAvailableScreenType);
                                if (insuranceProductsAvailableScreenType.ID < 1)
                                {
                                    ts.Dispose();
                                    LogHelper.WriteLog("新增机型承保条件失败：" + JsonConvert.SerializeObject(insuranceProductsAvailableScreenType), LogType.remark);
                                    return false;
                                }
                            }
                        }
                        //2.5 添加信息收集配置
                        if (cellPhoneItem.InsuranceProductsInfoCollectItemMappings != null)
                        {
                            foreach (var collectItemMappingsItem in cellPhoneItem.InsuranceProductsInfoCollectItemMappings)
                            {

                                collectItemMappingsItem.CellPhoneServiceProjectID = cellModel.ID;
                                var collectItemMappingsResult = collectItemMappings.AddEntities(collectItemMappingsItem);
                                if (collectItemMappingsResult.ID < 1)
                                {
                                    ts.Dispose();
                                    LogHelper.WriteLog("新增信息收集配置失败：" + JsonConvert.SerializeObject(collectItemMappingsItem), LogType.remark);
                                    return false;
                                }
                            }
                        }
                        //2.6 添加审核项目配置
                        if (cellPhoneItem.InsuranceProductsAuditProjectConfigurationMappings != null)
                        {
                            foreach (var auditProjectConfigurationMappingsItem in cellPhoneItem.InsuranceProductsAuditProjectConfigurationMappings)
                            {

                                auditProjectConfigurationMappingsItem.CellPhoneServiceProjectID = cellModel.ID;
                                var auditProjectConfigurationMappingsResult = auditProjectConfigurationMappings.AddEntities(auditProjectConfigurationMappingsItem);
                                if (auditProjectConfigurationMappingsResult.ID < 1)
                                {
                                    ts.Dispose();
                                    LogHelper.WriteLog("新增审核项目配置失败：" + JsonConvert.SerializeObject(auditProjectConfigurationMappingsItem), LogType.remark);
                                    return false;
                                }
                            }
                        }
                    }
                    #endregion

                    ts.Complete();
                    return true;
                }
                catch (Exception ex)
                {
                    ts.Dispose();
                    LogHelper.WriteLog("新增基础产品_新异常：" + ex.ToString() + "；\r\n 产品信息：" + JsonConvert.SerializeObject(param), LogType.error);
                    return false;
                }
            }
        }
        #endregion


        #region 修改基础产品_新 + bool UpdateInsuranceProductsBase(InsuranceProductsBase param)
        /// <summary>
        /// 修改基础产品_新
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public bool UpdateInsuranceProductsBase(InsuranceProductsBase param)
        {
            InsuranceProducts insuranceProduct = LoadByID(param.ID);
            if (insuranceProduct == null)
                return false;
            insuranceProduct.Coverage = param.Coverage;
            insuranceProduct.ProductName = param.ProductName;
            insuranceProduct.PolicyPeriodYear = param.PolicyPeriodYear;
            insuranceProduct.PolicyPeriodMonth = param.PolicyPeriodMonth;
            insuranceProduct.PolicyPeriodDay = param.PolicyPeriodDay;
            insuranceProduct.LastModifyBy = param.LastModifyBy;
            insuranceProduct.WaitDays = param.WaitDays;
            insuranceProduct.LastModifyTime = param.LastModifyTime;
            insuranceProduct.IsAvailable = param.IsAvailable;


            var cellPhoneService = ServiceFactory.CellPhoneServiceProjectServiceInstance; //手机维修服务
            var screenSPs = ServiceFactory.InsuranceProductScreenSPsServiceInstance;   //2.1 配件等级
            var warrantyMaintenanceTypes = ServiceFactory.InsuranceProductWarrantyMaintenanceTypesServiceInstance; //2.2 添加可维修方式
            var modelType = ServiceFactory.InsuranceProductsAvailableModelTypeServiceInstance;   //2.3 添加机型承保条件
            var screenType = ServiceFactory.InsuranceProductsAvailableScreenTypeServiceInstance;  //2.4 添加屏幕类型承保条件
            var collectItemMappings = ServiceFactory.InsuranceProductsInfoCollectItemMappingsServiceInstance;  //2.5 添加信息收集配置
            var auditProjectConfigurationMappings = ServiceFactory.InsuranceProductsAuditProjectConfigurationMappingsServiceInstance;  //2.6 添加审核项目配置

            var cellPhoneIds = cellPhoneService.LoadEntities(t => t.InsuranceProductsID == insuranceProduct.ID).Select(t => t.ID).ToList();
            string sql = "";
            if (cellPhoneIds != null && cellPhoneIds.Count() > 0)
            {
                string ids = "";
                ids = cellPhoneIds.Aggregate(ids, (current, rows) => current + (rows + ","));
                ids = ids.TrimEnd(',');
                //删除 配件等级 可维修方式 机型承保条件 屏幕类型承保条件信息收集配置 审核项目配置
                sql += "DELETE FROM InsuranceProductScreenSPs WHERE CellPhoneServiceProjectID IN(" + ids + ") AND 1=@Paramter;";
                sql += "DELETE FROM InsuranceProductWarrantyMaintenanceTypes WHERE CellPhoneServiceProjectID IN(" + ids + ");";
                sql += "DELETE FROM InsuranceProductsAvailableModelType WHERE CellPhoneServiceProjectID IN(" + ids + ");";
                sql += "DELETE FROM InsuranceProductsAvailableScreenType WHERE CellPhoneServiceProjectID IN(" + ids + ");";
                sql += "DELETE FROM InsuranceProductsInfoCollectItemMappings WHERE CellPhoneServiceProjectID IN(" + ids + ");";
                sql += "DELETE FROM InsuranceProductsAuditProjectConfigurationMappings WHERE CellPhoneServiceProjectID IN(" + ids + ");";
                sql += "DELETE FROM CellPhoneServiceProject WHERE InsuranceProductsID = " + insuranceProduct.ID + ";";
            }

            using (TransactionScope ts = new TransactionScope())
            {
                try
                {
                    //1、修改保险基础产品信息
                    if (!UpdateEntities(insuranceProduct))
                    {
                        ts.Dispose();
                        return false;
                    }
                    //删除所有的子表信息
                    if (!string.IsNullOrEmpty(sql))
                    {
                        bool r = ExcuteSql(sql, new SqlParameter[] { new System.Data.SqlClient.SqlParameter("Paramter", 1) }) > 0;
                        if (!r)
                        {
                            LogHelper.WriteLog("修改_删除所有的子表信息失败：" + sql, LogType.remark);
                            ts.Dispose();
                            return false;
                        }
                    }

                    //2、添加手机维修服务信息
                    #region 添加手机维修服务信息
                    foreach (CellPhoneServiceProjectParam cellPhoneItem in param.CellPhoneServiceProject)
                    {
                        CellPhoneServiceProject cellModel = new CellPhoneServiceProject()
                        {
                            InsuranceProductsID = insuranceProduct.ID,
                            InsuranceDevice = cellPhoneItem.InsuranceDevice,
                            InsuranceDeviceType = cellPhoneItem.InsuranceDeviceType,
                            ModelTypeJudgeType = cellPhoneItem.ModelTypeJudgeType,
                            NewMachineInsuranceTimeLimit = cellPhoneItem.NewMachineInsuranceTimeLimit,
                            ProductAmountFrom = cellPhoneItem.ProductAmountFrom,
                            ProductAmountTo = cellPhoneItem.ProductAmountTo,
                            ScreenTypeJudgeType = cellPhoneItem.ScreenTypeJudgeType,
                            ServiceProjectId = cellPhoneItem.ServiceProjectId,
                            ServiceTimes = cellPhoneItem.ServiceTimes,
                            UseModelTypeOption = cellPhoneItem.UseModelTypeOption,
                            UsePriceOption = cellPhoneItem.UsePriceOption,
                            UseScreenTypeOption = cellPhoneItem.UseScreenTypeOption,
                            InsuredAmount = cellPhoneItem.InsuredAmount
                        };
                        cellModel = cellPhoneService.AddEntities(cellModel);
                        if (cellModel.ID < 1)
                        {
                            ts.Dispose();
                            LogHelper.WriteLog("修改_新增手机维修服务信息失败：" + JsonConvert.SerializeObject(cellModel), LogType.remark);
                            return false;
                        }
                        //2.1 添加配件等级
                        if (cellPhoneItem.AccessoriesLevelList != null)
                        {
                            foreach (InsuranceProductScreenSPs screenSPsItem in cellPhoneItem.AccessoriesLevelList)
                            {
                                screenSPsItem.CellPhoneServiceProjectID = cellModel.ID;
                                var screenSPsRe = screenSPs.AddEntities(screenSPsItem);
                                if (screenSPsRe.ID < 1)
                                {
                                    ts.Dispose();
                                    LogHelper.WriteLog("修改_新增配件等级失败：" + JsonConvert.SerializeObject(screenSPsItem), LogType.remark);
                                    return false;
                                }

                            }
                        }
                        //2.2 添加可维修方式
                        if (cellPhoneItem.InsuranceWarrantyMaintenanceTypeList != null)
                        {
                            foreach (int warrantyMaintenanceTypeItem in cellPhoneItem.InsuranceWarrantyMaintenanceTypeList)
                            {
                                InsuranceProductWarrantyMaintenanceTypes warrantyMaintenanceTypeModel = new InsuranceProductWarrantyMaintenanceTypes
                                {
                                    CellPhoneServiceProjectID = cellModel.ID,
                                    InsuranceWarrantyMaintenanceTypeId = warrantyMaintenanceTypeItem
                                };
                                warrantyMaintenanceTypeModel = warrantyMaintenanceTypes.AddEntities(warrantyMaintenanceTypeModel);
                                if (warrantyMaintenanceTypeModel.ID < 1)
                                {
                                    ts.Dispose();
                                    LogHelper.WriteLog("修改_新增可维修方式失败：" + JsonConvert.SerializeObject(warrantyMaintenanceTypeModel), LogType.remark);
                                    return false;
                                }
                            }
                        }
                        //2.3 添加机型承保条件
                        if (cellPhoneItem.InsuranceProductsAvailableModelType != null)
                        {
                            foreach (int warrantyMaintenanceTypeItem in cellPhoneItem.InsuranceProductsAvailableModelType)
                            {
                                InsuranceProductsAvailableModelType insuranceProductsAvailableModelType = new InsuranceProductsAvailableModelType
                                {
                                    CellPhoneServiceProjectID = cellModel.ID,
                                    ModelID = warrantyMaintenanceTypeItem
                                };
                                insuranceProductsAvailableModelType = modelType.AddEntities(insuranceProductsAvailableModelType);
                                if (insuranceProductsAvailableModelType.ID < 1)
                                {
                                    ts.Dispose();
                                    LogHelper.WriteLog("修改_新增机型承保条件失败：" + JsonConvert.SerializeObject(insuranceProductsAvailableModelType), LogType.remark);
                                    return false;
                                }
                            }
                        }
                        //2.4 添加屏幕类型承保条件
                        if (cellPhoneItem.InsuranceProductsAvailableScreenType != null)
                        {
                            foreach (byte screenTypeItem in cellPhoneItem.InsuranceProductsAvailableScreenType)
                            {
                                InsuranceProductsAvailableScreenType insuranceProductsAvailableScreenType = new InsuranceProductsAvailableScreenType
                                {
                                    CellPhoneServiceProjectID = cellModel.ID,
                                    ScreenTypeCode = screenTypeItem
                                };
                                insuranceProductsAvailableScreenType = screenType.AddEntities(insuranceProductsAvailableScreenType);
                                if (insuranceProductsAvailableScreenType.ID < 1)
                                {
                                    ts.Dispose();
                                    LogHelper.WriteLog("修改_新增机型承保条件失败：" + JsonConvert.SerializeObject(insuranceProductsAvailableScreenType), LogType.remark);
                                    return false;
                                }
                            }
                        }
                        //2.5 添加信息收集配置
                        if (cellPhoneItem.InsuranceProductsInfoCollectItemMappings != null)
                        {
                            foreach (var collectItemMappingsItem in cellPhoneItem.InsuranceProductsInfoCollectItemMappings)
                            {

                                collectItemMappingsItem.CellPhoneServiceProjectID = cellModel.ID;
                                var collectItemMappingsResult = collectItemMappings.AddEntities(collectItemMappingsItem);
                                if (collectItemMappingsResult.ID < 1)
                                {
                                    ts.Dispose();
                                    LogHelper.WriteLog("修改_新增信息收集配置失败：" + JsonConvert.SerializeObject(collectItemMappingsItem), LogType.remark);
                                    return false;
                                }
                            }
                        }
                        //2.6 添加审核项目配置
                        if (cellPhoneItem.InsuranceProductsAuditProjectConfigurationMappings != null)
                        {
                            foreach (var auditProjectConfigurationMappingsItem in cellPhoneItem.InsuranceProductsAuditProjectConfigurationMappings)
                            {

                                auditProjectConfigurationMappingsItem.CellPhoneServiceProjectID = cellModel.ID;
                                var auditProjectConfigurationMappingsResult = auditProjectConfigurationMappings.AddEntities(auditProjectConfigurationMappingsItem);
                                if (auditProjectConfigurationMappingsResult.ID < 1)
                                {
                                    ts.Dispose();
                                    LogHelper.WriteLog("修改_新增审核项目配置失败：" + JsonConvert.SerializeObject(auditProjectConfigurationMappingsItem), LogType.remark);
                                    return false;
                                }
                            }
                        }
                    }
                    #endregion

                    ts.Complete();
                    return true;
                }
                catch (Exception ex)
                {
                    ts.Dispose();
                    LogHelper.WriteLog("修改基础产品_新异常：" + ex.ToString() + "；\r\n 产品信息：" + JsonConvert.SerializeObject(param), LogType.error);
                    return false;
                }
            }
        }
        #endregion

    }
}
